package cn.cdtu.tibetplan.controller;

import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.cdtu.tibetplan.service.MenuService;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<Menu> findAll() {
        return menuService.findAll();
    }

    @RequestMapping("/list")
    public String list() {
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(Menu menu, int currentPage, int pageSize, Model model) {
        PageResult result = menuService.findPage(menu, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/Menu/index";
    }

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(int page, int rows) {
        return menuService.findPage(page, rows);
    }

    @RequestMapping("/addMenu")
    public String addMenu(Model model) {
        Menu m = new Menu();
        m.setPid(0);
        List<Menu> parents = menuService.findByPId(m);
        model.addAttribute("parents",parents);
        List<Menu> list = menuService.findAll();
        model.addAttribute("menu", list);
        return "/Menu/add";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model) {
        Menu menu = menuService.findOne(id.longValue());
        model.addAttribute("show", menu);
        return "/Menu/show";
    }

    /**
     * 增加
     *
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        Menu m = new Menu();
        m.setPid(0);
        List<Menu> parents = menuService.findByPId(m);
        model.addAttribute("parents",parents);
        Menu menu = menuService.findOne(id.longValue());
        model.addAttribute("edit", menu);
        return "/Menu/edit";
    }

    /**
     * 修改
     *
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result update(Menu menu) {
        try {
            menuService.update(menu);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public Menu findOne(Long id) {
        return menuService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            menuService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

//    /**
//     * 查询+分页
//     *
//     * @param
//     * @param page
//     * @param rows
//     * @return
//     */
//    @RequestMapping("/search")
//    public PageResult search(@RequestBody Menu menu, int page, int rows) {
//        return menuService.findPage(menu, page, rows);
//    }

}
